package com.ant.storage.service.impl;

import com.ant.storage.common.ResultCode;
import com.ant.storage.common.ResultPage;
import com.ant.storage.handler.AntException;
import com.ant.storage.mapper.InventoryMapper;
import com.ant.storage.mapper.OtherOutMapper;
import com.ant.storage.pojo.OtherOut;
import com.ant.storage.pojo.OtherOutCommodity;
import com.ant.storage.service.ICityService;
import com.ant.storage.service.IOtherOutService;
import com.ant.storage.utils.NullInspectUtil;
import com.ant.storage.common.PageRecv;
import com.ant.storage.common.ResultVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OtherOutServiceImpl extends ServiceImpl<OtherOutMapper,OtherOut> implements IOtherOutService {

    @Autowired(required = false)
    OtherOutMapper otherOutMapper;

    @Autowired(required = false)
    InventoryMapper inventoryMapper;

    @Autowired
    ICityService cityService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo addOtherOut(OtherOut otherOut) throws NoSuchFieldException {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        otherOut.setWarehouse_out_time(simpleDateFormat.format(date));
        String str01 = NullInspectUtil.ClassnullInspect(otherOut,"other_out_id","remarks","start_time","end_time","city","dept");
        if(str01 != null){
           NullInspectUtil.myAnnotationError(otherOut,str01);
        }
        if(!this.save(otherOut)){
            throw new AntException(ResultCode.SQL_INSERT_ERR);
        }
        for (OtherOutCommodity otherOutCommodity:otherOut.getList()) {
            if(otherOutCommodity.getCommodity().getCommodityId() == null||otherOutCommodity.getCommodity().getCommodityId()<=0){
                throw new AntException(ResultCode.GOOD_ID_ERROR);
            }
            if(otherOutCommodity.getWarehouse_out_number() == null||otherOutCommodity.getWarehouse_out_number()<=0){
                throw new AntException(ResultCode.GOOD_NUMBER_ERROR);
            }
            otherOutMapper.addOtherOutCommodity(otherOut.getOther_out_id(),otherOutCommodity.getCommodity().getCommodityId(),otherOutCommodity.getWarehouse().getId(),otherOutCommodity.getWarehouse_out_number());
            //扣减对应库存
            inventoryMapper.stockSubtract(otherOutCommodity.getWarehouse().getId(),otherOutCommodity.getCommodity().getCommodityId(),otherOutCommodity.getWarehouse_out_number());
        }
        return ResultVo.success("ok");
    }

    @Override
    public ResultPage queryOtherOut(PageRecv<OtherOut> pageRecv)   {
        Map map = new HashMap<>();
        if (!(pageRecv.getData()==null)){
            map = NullInspectUtil.ClassChangeMapNotNull(pageRecv.getData());
        }
        Integer page = (pageRecv.getCurrentPage()-1)*pageRecv.getPageSize();
        Integer limit = pageRecv.getCurrentPage()*pageRecv.getPageSize();
        List<OtherOut>list = otherOutMapper.queryOtherOut(page,limit,map);
        for (OtherOut otherOut:list) {
            otherOut.getCity().setName(cityService.idQuery(otherOut.getCity_id()));
            otherOut.getDept().setName(cityService.idQuery(otherOut.getDepartment_id()));
        }
        Integer count = otherOutMapper.countOtherOut(map);
        return ResultPage.success("ok",list,count);
    }

}
